package com.java.system.controller;

import com.java.system.entity.Area;
import com.java.system.entity.Camera;
import com.java.system.entity.Result;
import com.java.system.entity.Type;
import com.java.system.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  类型控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    ITypeService typeService;
    @GetMapping("/typeList")
    public Result getArea(){
        List<Type> typeList = typeService.getType();
        if(!StringUtils.isEmpty(typeList)){
            Result result=Result.success("获取类型列表成功");
            result.put("data",typeList);
            return result;
        }
        else {
            return Result.error("获取类型信息失败");
        }
    }
    @GetMapping("/cameraList/{id}")
    public Result getCameraListById(@PathVariable("id")Integer id){
        List<Camera> cameraList = typeService.getCameraListById(id);
        int size = cameraList.size();
        if(!StringUtils.isEmpty(cameraList)){
            Result result=Result.success("获取摄像头列表成功");
            result.put("data",cameraList);
            result.put("size",size);
            return result;
        }
        else {
            return Result.error("获取摄像头信息失败");
        }

    }
    @PostMapping("/add")
    public Result insertType(@RequestBody Type type){
        typeService.insertType(type);
        return Result.success("增加类型成功");

    }
    @GetMapping("/{id}")
    public Result getTypeById(@PathVariable("id")Integer id){
        Type type= typeService.getTypeById(id);
        if(type!=null){
            Result result=Result.success("获取类型成功");
            result.put("data",type);
            return result;
        }
        else {
            return Result.error("获取类型失败");
        }

    }
    @PutMapping
    public Result updateType(@RequestBody Type type){
        typeService.updateType(type);
        return Result.success("更新类型信息成功");

    }
    @DeleteMapping("/{id}")
    public Result deleteType(@PathVariable("id")Integer id){
        typeService.deleteType(id);
        return Result.success("删除类型信息成功");
    }
}
